package com.cdd.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cdd.user.entity.UserRole;
import com.cdd.user.mapper.UserRoleMapper;
import com.cdd.user.service.UserRoleService;
import com.common.untils.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 *
 * @author lenovo
 * @version 1.0
 * @since 2022-09-06  14:50:00
 */
@Service
public class UserRoleServiceImpl implements UserRoleService {
    @Autowired
    private UserRoleMapper userRoleMapper;
    //分配角色
    @Override
    public CommonResult fenUserRole(String userId, List<String> idlist) {
        System.out.println(userId+"-----------------");
        System.out.println(idlist+"----------------");
        //先删除用户的角色根据用户id
        QueryWrapper<UserRole> wrapper=new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        wrapper.eq("user_id",userId);
        int delete = userRoleMapper.delete(wrapper);
            for (String roleId:idlist) {
                System.out.println(roleId+"====================");
                //根据用户添加角色
                UserRole userRole=new UserRole();
                userRole.setIsDelete(0);
                userRole.setUserId(userId);
                userRole.setRoleId(roleId);
                userRoleMapper.insert(userRole);
            }

        return new CommonResult(2000,"角色添加成功",null);
    }
}
